<template>
  <section class="jumbotron">
    <h3 class="jumbotron-heading">Search Github Users</h3>
    <div>
      <input type="text" placeholder="enter the name you search" v-model="keyword" />
      &nbsp;<button @click="handClick">Search</button>
    </div>
  </section>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Search',
  data() {
    return {
      keyword: '',
    }
  },
  methods: {
    handClick() {
      axios.get(`https://api.github.com/search/users?q=${this.keyword}`).then(
        (req) => {
          console.log('请求成功', req.data);
          // 触发自定义事件
          this.$bus.$emit('sendVal', req.data.items)
        },
        (err) => {
          console.log('请求失败', err);
        }
      )
    }
  },
  mounted() {
    this.$bus.$on('send', this.handClick)
  },
  beforeDestroy() {
    this.$bus.$off('send')
  },
}
</script>

<style scoped>
.album {
  min-height: 50rem;
  /* Can be removed; just added for demo purposes */
  padding-top: 3rem;
  padding-bottom: 3rem;
  background-color: #f7f7f7;
}
</style>